
package cn.fulong.sso.client.utils;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;


@Slf4j
public class RequestUtil {

    public static String getHeader(HttpServletRequest request, String headerName) {
        return request.getHeader(headerName);
    }

    /**
     * @return 请求来源
     */
    public static String getReferer(HttpServletRequest request) {
        return getHeader(request, "Referer");
    }

    /**
     * @return 客户端类型
     */
    public static String getUserAgent(HttpServletRequest request) {
        return getHeader(request, "User-Agent");
    }

    /**
     * @return 是否微信客户端
     */
    public static boolean isUserAgentWechat(HttpServletRequest request) {
        String userAgnt = getHeader(request, "User-Agent");
        if (StringSelfUtil.isBlank(userAgnt)) {
            log.warn("【User-Agent】为空");
            return false;
        }
        return (userAgnt.toLowerCase().indexOf("micromessenger") > -1);
    }


    public static String getMethod(HttpServletRequest request) {
        return request.getMethod();
    }

    /**
     * @param httpRequest
     * @return 是否ajax请求
     */
    public static boolean isAjax(HttpServletRequest httpRequest) {
        String xRequestedWith = httpRequest.getHeader("x-requested-with");
        return StringSelfUtil.equalsIgnoreTrimCase("XMLHttpRequest",xRequestedWith);
    }


    public static boolean isMethodPost(HttpServletRequest httpRequest) {
        return StringSelfUtil.equalsIgnoreCase ("POST",getMethod(httpRequest));
    }
}
